package com.example.xianji.adapter;

import java.util.ArrayList;

import com.example.xianji.activity.Main_XuQiu_DetailsActivity;
import com.example.xianji.R;
import com.example.xianji.bean.XuQiu;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class XuQiu_Adapter extends BaseAdapter {
    /************************需求大厅列表适配器************************/
    private Context mContext;
    private ArrayList<XuQiu> list;


    public XuQiu_Adapter(Context mContext, ArrayList<XuQiu> list) {
        super();
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;
        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(mContext, R.layout.xuqiu_listview_layout, null);

            mHolder.textview_xuqiu = (TextView) convertView.findViewById(R.id.textview_xuqiu);
            mHolder.textview_xuqiu_money = (TextView) convertView.findViewById(R.id.textview_xuqiu_money);
            // mHolder.iamge_video=(ImageView)convertView.findViewById(R.id.iamge_video);
            convertView.setTag(mHolder);
        }

        mHolder = (Holder) convertView.getTag();
        mHolder.textview_xuqiu.setText(list.get(position).getTitle());
        if (list.get(position).getBudget().equals("negotiable")) {
            mHolder.textview_xuqiu_money.setText("面议");
        } else {
            mHolder.textview_xuqiu_money.setText(list.get(position).getBudget_val());

        }
        final int a = position;
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Main_XuQiu_DetailsActivity.class);
                intent.putExtra("xuqiu_id", list.get(a).getId());
                mContext.startActivity(intent);
            }
        });
		/*final ImageView image= mHolder.iamge_video;
		final Handler handler =new Handler(){

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				
				Log.i("3333333","222222");
				if(msg.arg1==1){
				image.setImageBitmap((Bitmap)msg.obj);}
				
			}
			
			
		};
		
		XJApplication.cachThreadPool.execute(new Runnable() {
			
			@Override
			public void run() {
				 Log.i("jinijinij","jinijinij");

                 MediaMetadataRetriever retriever = new MediaMetadataRetriever();

                 try {
                     if (Build.VERSION.SDK_INT >= 14) {
                         retriever.setDataSource(list.get(a).getUrl(), new HashMap<String, String>());
                     } else {
                         retriever.setDataSource(list.get(a).getUrl());
                     }
                     
                   Bitmap bitmap = retriever.getFrameAtTime();

                   Message mess=handler.obtainMessage();
                   
                   mess.obj=bitmap;
                   mess.arg1=1;
                   handler.sendMessage(mess);
                     Log.i("2222222",bitmap+"222222");
                 } catch (RuntimeException ex) {
                     // Assume this is a corrupt video file.
                 } finally {
                     try {
                         retriever.release();
                     } catch (RuntimeException ex) {
                         // Ignore failures while cleaning up.
                     }
                 }
			}
		});*/

        return convertView;
    }

    class Holder {
        TextView textview_xuqiu;
        TextView textview_xuqiu_money;
        ImageView iamge_video;
    }
}
